package com.example.administrator.base.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.UiSettings;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.example.administrator.base.R;
import com.example.administrator.base.baseActivity.BaseActivity;
import com.example.administrator.base.util.GPSUtil;
import com.example.administrator.base.util.SharedPreferencesUtils;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.Response;

public class AddPoint extends BaseActivity {

    @InjectView(R.id.Toolbar)
    android.support.v7.widget.Toolbar Toolbar;
    @InjectView(R.id.btn_next)
    Button btnNext;
    double lat, lon;
    @InjectView(R.id.map)
    MapView map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_point);
        ButterKnife.inject(this);
        map.onCreate(savedInstanceState);
        UiSettings mUiSettings = map.getMap().getUiSettings();
        mUiSettings.setZoomControlsEnabled(false);
        mUiSettings.setRotateGesturesEnabled(false);

        Toolbar.setTitle("赳猎人");
        Toolbar.setSubtitle("微调定位点");
        setSupportActionBar(Toolbar);
        Toolbar.setNavigationIcon(R.drawable.backbar);
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        lat = Double.valueOf(String.valueOf(SharedPreferencesUtils.getParam(AddPoint.this, "latitude", "1")));
        lon = Double.valueOf(String.valueOf(SharedPreferencesUtils.getParam(AddPoint.this, "longtitude", "1")));
        resetLocation(lat, lon, 18);
        map.getMap().setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
            @Override
            public void onCameraChange(CameraPosition cameraPosition) {

            }

            @Override
            public void onCameraChangeFinish(CameraPosition cameraPosition) {
                double[] doubles = GPSUtil.gcj02_To_Bd09(cameraPosition.target.latitude, cameraPosition.target.longitude);
                lat = doubles[0];
                lon = doubles[1];
                Log.i("2121541",""+lat);
            }
        });
    }

    @OnClick(R.id.btn_next)
    public void onViewClicked() {
        Intent intent = new Intent(AddPoint.this, AddpointTwo.class);
        intent.putExtra("lat", String.valueOf(lat));
        intent.putExtra("lon", String.valueOf(lon));
        startActivity(intent);
        finish();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        map.onSaveInstanceState(outState);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        map.onDestroy();

    }

    @Override
    protected void onResponseSuccess(Response response, String action) {

    }

    @Override
    protected void onResponseFailure(Throwable e, String action) {

    }

    @Override
    protected void onPause() {
        super.onPause();
        map.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        map.onResume();

    }
    /*移动初始地图镜头*/
    private void resetLocation(double v1, double v2, int x) {
        double[] doubles = GPSUtil.bd09_To_Gcj02(v1, v2);
        LatLng latlon = new LatLng(doubles[0], doubles[1]);
       map.getMap().animateCamera(CameraUpdateFactory.newLatLngZoom(latlon, x), 1000, null);

    }

}
